package com.ccb.sc.member.service;

import com.ccb.sc.member.convert.AppMemberConvert;
import com.ccb.sc.member.vo.AppMemberUpdateVo;
//psl import com.ccb.sc.common.bind.exception.BizException;
import com.ccb.sc.common.code.BizCodeFace;
import com.ccb.sc.common.code.ErrorCode;
import com.ccb.sc.common.modules.common.service.IDistrictService;
import com.ccb.sc.common.modules.common.vo.AddressInfoVo;
import com.ccb.sc.common.modules.member.entity.Member;
import com.ccb.sc.common.modules.member.enums.MemberStatus;
import com.ccb.sc.common.modules.member.service.MemberService;
import com.ccb.sc.common.modules.setting.service.ISettingsService;
import com.ccb.sc.common.util.PasswordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * @author ken
 * @version 1.0
 * @date 2020-09-16
 */
@Slf4j
@Service
public class AppMemberService extends MemberService {

    private IDistrictService districtService;
    private ISettingsService settingsService;

    public Member registerByMa(Member member, String inviteId) {
        member.setStatus(MemberStatus.AVAILABLE);
        if (StringUtils.hasText(inviteId)) {
            try {
                Long inviteIdLong = Long.valueOf(inviteId);
                Member inviteMember = this.getById(inviteIdLong);
                if (inviteMember != null) {
                    member.setInviteId(inviteIdLong);
                }
            }catch (Exception e) {
                log.error("inviteId[{}] parse error", inviteId, e);
            }
        }
        this.save(member);
        return member;
    }


    @Transactional(rollbackFor = Exception.class)
    public Member register(String phone, String password, BigDecimal latitude, BigDecimal longitude) {
        if (!StringUtils.hasText(phone)) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("手机号不能为空"));
        }
        if (!StringUtils.hasText(password)) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("密码不能为空"));
        }
        Member existMember = this.findByPhone(phone);
        if (existMember != null) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.DATE_NULL).message("手机号已注册"));
        }
        Member member = new Member();
        String salt = PasswordUtil.generateSalt();
        String encodePassword = PasswordUtil.encodePassword(password, salt);
        member.setPhone(phone);
        member.setNickname(phone);
        member.setSalt(salt);
        member.setPassword(encodePassword);
        member.setLatitude(latitude);
        member.setLongitude(longitude);

        if (latitude != null && longitude != null) {
            AddressInfoVo addressInfoVo = this.districtService.locateDistrict(latitude, longitude);
            member.setRegionId(addressInfoVo.getCityId());
        }

        member.setStatus(MemberStatus.AVAILABLE);
        boolean result = this.save(member);
        if (!result) {
            //psl throw new BizException(BizCodeFace.createBizCode(ErrorCode.FAIL).message("系统繁忙，请重试"));
        }
        return member;
    }

    public boolean updatePassword(Long memberId, String password) {
        Member member = this.getById(memberId);
        String salt = PasswordUtil.generateSalt();
        String encodePassword = PasswordUtil.encodePassword(password, salt);
        member.setSalt(salt);
        member.setPassword(encodePassword);
        member.setVersion(member.getVersion());
        return this.updateById(member);
    }

    public boolean updateInfo(Long memberId, AppMemberUpdateVo updateVo) {
        Member member = AppMemberConvert.INSTANCE.updateVo2Entity(updateVo);
        member.setId(memberId);
        member.setVersion(member.getVersion());
        return this.updateById(member);
    }

    @Autowired
    public void setDistrictService(IDistrictService districtService) {
        this.districtService = districtService;
    }

    @Autowired
    public void setSettingsService(ISettingsService settingsService) {
        this.settingsService = settingsService;
    }

}
